import { loginAPI } from '@/api/user'

export default {
  namespaced: true,
  state: {
    loginInfo: {},
    tokenTime: '',
    clientToken: ''
  },
  mutations: {
    LOGIN(state, res) {
      state.loginInfo = JSON.parse(JSON.stringify(res))
    },
    CLIENTTOKEN(state, res) {
      state.clientToken = res
    },
    REMOVELOGIN(state) {
      state.loginInfo = {}
    },
    TOKENTIME(state, time) {
      state.tokenTime = time
    }
  },
  actions: {
    async login({ commit }, res) {
      const { data } = await loginAPI(res)
      if (data.success) {
        commit('LOGIN', data)
        commit('TOKENTIME', Date.now())
        return 'ok'
      } else {
        return Promise.reject('验证码错误')
      }
    },
    logout({ commit }) {
      commit('REMOVELOGIN')
    }
  }
}

